<?php

namespace App\Console\Commands;

use App\Models\User;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     *
     *
     *
     *
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        return $this->updateUserAvatar();
    }

    public function updateUserAvatar()
    {

        $users = User::limit(3000)->where('id','>',153)->get();
        foreach ($users as $user) {
            $client = new Client();
            try {
                if($user->avatar == '') continue;
                $result = $client->get('http:' . $user->avatar);
                $this->info($user->id);
                Log::info('last_user_id:'.$user->id);
                continue;
                //有头像 跳过
            } catch (\Exception $exception) {
                $this->info('错误异常:' . '用户:' . $user->email . '头像缺失！');
                //替换默认图片操作
                $user->avatar = '';
                $user->save();
                $this->info($user->id);
                Log::info('last_user_id:'.$user->id);
            }

        }
        dd('ok');
    }


    public function checkTopic()
    {
        $node = DB::connection('mysql')->table('ix_forum_post')->get();
        $node_ids = $node->pluck('id');
        $topics = DB::connection('mysql')->table('comm_topic')->whereNotIn('node_id', $node_ids)->get();

        dd($topics);

    }


    public function migrateReply()
    {
        $cur = Cache::get('cur_reply', 0);
        // dd($cur);
        $replies = DB::connection('mysql')->table('comm_reply')->where('reply_id', 0)->where('id', '>', $cur)->limit(10000)->get();

        foreach ($replies as $reply) {
            $topic = DB::connection('mysql')->table('comm_topic')->where('id', $reply->topic_id)->value('user_id');
            $reply->ref_user = $topic;
            DB::connection('mysql')->table('comm_reply')->where('id', $reply->id)->update(['ref_user' => $topic]);
            $cur = Cache::increment('cur_reply', 1);
            $this->info('处理' . $cur . '条数据');
        }

        dd('ok');

    }


    public function migrateNode()
    {
        $members = DB::connection('ix')
            ->select("SELECT * FROM `ix_forum_forum` m LEFT JOIN `ix_forum_forumfield` f USING (`fid`) WHERE m.`fid` > ? ORDER BY m.`fid` LIMIT 65", [1]);
        $count = 0;

        foreach ($members as $row) {
            $a_fields = array(
                'id' => $row->fid,
                'code' => '',
                'title' => $row->name,
                'topic_count' => 0,
                'today_topic_count' => 0,
                'reply_count' => 0,
                'today_reply_count' => 0,
            );
            if ($row->type == 'forum') {
                $a_fields['cluster_id'] = $row->fup;
                $a_fields['description'] = $row->description;
                $a_fields['icon'] = '';
                //www.pixcn.cn/data/attachment/
                if ($row->icon) {
                    $a_parts = preg_split('#[/_]#', $row->icon);
                    $a_fields['icon'] = '//a.pixcn.cn/old/' . $a_parts[1] . '/' . $row->icon;
                }
                $a_fields['background'] = '';
                if ($row->banner) {
                    $a_parts = preg_split('#[/_]#', $row->banner);
                    $a_fields['background'] = '//a.pixcn.cn/old/' . $a_parts[1] . '/' . $row->banner;
                }
                $a_fields['status'] = 3 == $row->status ? 'invisible' : 'visible';
                $a_fields['user_id'] = 1;
                $a_fields['manger'] = 1;
                $a_fields['createTime'] = date('Y-m-d H:i:s', time());
                $a_fields['op_user'] = '';
                $a_fields['op_time'] = date('Y-m-d H:i:s', time());
                $a_fields['is_pin'] = 0;
                DB::connection('mysql')->table('comm_node')->insert($a_fields);
            }
            $count += 1;
            $this->info('成功处理:' . $count . '条数据');
        }

    }


}
